package bb.lanxing.model;

import java.util.ArrayList;

import org.litepal.LitePal;

import android.content.ContentProvider;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;

import bb.lanxing.App;
import gov.nist.core.Separators;

public class WorkoutContentProvider extends ContentProvider {
    public static final String AUTHORITY;
    public static final Uri AUTHORITY_URI;
    public static final Uri PACE_POINT_URI;
    public static final String PATH_PACE_POINT = "pacepoint";
    public static final String PATH_TRACK_POINT = "trackpoint";
    public static final String PATH_WORKOUT = "workout";
    public static final String QUERY_PARAM_GROUP_BY = "group_by";
    public static final String QUERY_PARAM_LIMIT = "limit";
    public static final String QUERY_PARAM_OFFSET = "offset";
    public static final Uri TRACK_POINT_URI;
    public static final Uri WORKOUT_URI;
    private static final int PACE_POINT = 5;
    private static final int PACE_POINT_ID = 6;
    private static final String TAG = "WorkoutContentProvider";
    private static final int TRACK_POINT = 3;
    private static final int TRACK_POINT_ID = 4;
    private static final int WORKOUT = 1;
    private static final int WORKOUT_ID = 2;
    private static final UriMatcher uriMatcher;

    static {
        AUTHORITY = App.getContext().getPackageName() + ".workoutcontentprovider";
        Uri parse = Uri.parse("content://" + AUTHORITY);
        AUTHORITY_URI = parse;
        WORKOUT_URI = Uri.withAppendedPath(parse, PATH_WORKOUT);
        TRACK_POINT_URI = Uri.withAppendedPath(parse, PATH_TRACK_POINT);
        PACE_POINT_URI = Uri.withAppendedPath(parse, PATH_PACE_POINT);
        UriMatcher uriMatcher2 = new UriMatcher(-1);
        uriMatcher = uriMatcher2;
        uriMatcher2.addURI(AUTHORITY, PATH_WORKOUT, 1);
        uriMatcher.addURI(AUTHORITY, "workout/#", 2);
        uriMatcher.addURI(AUTHORITY, PATH_TRACK_POINT, 3);
        uriMatcher.addURI(AUTHORITY, "trackpoint/#", 4);
        uriMatcher.addURI(AUTHORITY, PATH_PACE_POINT, 5);
        uriMatcher.addURI(AUTHORITY, "pacepoint/#", 6);
    }

    private ContentResolver contentResolver;

    @Override
    public boolean onCreate() {
        Context context = getContext();
        if (context != null) {
            this.contentResolver = context.getContentResolver();
            return true;

            // try {
            // if (SugarContext.getSugarContext() != null) {
            // return true;
            // }
            // SugarContext.init(context);
            // return true;
            // } catch (NullPointerException unused) {
            // SugarContext.init(context);
            // return true;
            // }

        }
        return false;
    }

    @Override
    public int delete(Uri uri, String str, String[] strArr) {
        int delete;
        ContentResolver contentResolver;
        SQLiteDatabase writableDB = getWritableDB();
        String str2 = "id=?";
        switch (uriMatcher.match(uri)) {
        case 1:
            delete = writableDB.delete(PATH_WORKOUT, str, strArr);
            break;
        case 2:
            String[] appendArgs = appendArgs(strArr, uri.getLastPathSegment());
            if (!TextUtils.isEmpty(str)) {
                str2 = Separators.LPAREN + str + ") AND id=?";
            }
            delete = writableDB.delete(PATH_WORKOUT, str2, appendArgs);
            break;
        case 3:
            delete = writableDB.delete(PATH_TRACK_POINT, str, strArr);
            break;
        case 4:
            String[] appendArgs2 = appendArgs(strArr, uri.getLastPathSegment());
            if (!TextUtils.isEmpty(str)) {
                str2 = Separators.LPAREN + str + ") AND id=?";
            }
            delete = writableDB.delete(PATH_TRACK_POINT, str2, appendArgs2);
            break;
        case 5:
            delete = writableDB.delete(PATH_PACE_POINT, str, strArr);
            break;
        case 6:
            String[] appendArgs3 = appendArgs(strArr, uri.getLastPathSegment());
            if (!TextUtils.isEmpty(str)) {
                str2 = Separators.LPAREN + str + ") AND id=?";
            }
            delete = writableDB.delete(PATH_PACE_POINT, str2, appendArgs3);
            break;
        default:
            return 0;
        }
        if (delete > 0 && (contentResolver = this.contentResolver) != null) {
            contentResolver.notifyChange(uri, null);
        }
        return delete;
    }

    @Override
    public String getType(Uri uri) {
        switch (uriMatcher.match(uri)) {
        case 1:
            return "vnd.android.cursor.dir/workout";
        case 2:
            return "vnd.android.cursor.item/workout";
        case 3:
            return "vnd.android.cursor.dir/track-point";
        case 4:
            return "vnd.android.cursor.item/track-point";
        case 5:
            return "vnd.android.cursor.dir/pace-point";
        case 6:
            return "vnd.android.cursor.item/pace-point";
        default:
            return null;
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues contentValues) {
        long insertWithOnConflict;
        ContentResolver contentResolver;
        SQLiteDatabase writableDB = getWritableDB();
        int match = uriMatcher.match(uri);
        if (match == 1) {
            insertWithOnConflict = writableDB.insertWithOnConflict(PATH_WORKOUT, null, contentValues, 5);
        } else if (match == 3) {
            insertWithOnConflict = writableDB.insertWithOnConflict(PATH_TRACK_POINT, null, contentValues, 5);
        } else if (match == 5) {
            insertWithOnConflict = writableDB.insertWithOnConflict(PATH_PACE_POINT, null, contentValues, 5);
        } else {
            throw new IllegalArgumentException("Unknown URI " + uri + " for insert");
        }
        Uri withAppendedId = ContentUris.withAppendedId(uri, insertWithOnConflict);
        if (insertWithOnConflict > 0 && (contentResolver = this.contentResolver) != null) {
            contentResolver.notifyChange(withAppendedId, null);
        }
        return withAppendedId;
    }

    private String[] appendArgs(String[] strArr, String str) {
        String[] strArr2;
        if (strArr != null) {
            strArr2 = new String[strArr.length + 1];
            System.arraycopy(strArr, 0, strArr2, 0, strArr.length);
        } else {
            strArr2 = new String[1];
        }
        strArr2[strArr2.length - 1] = str;
        return strArr2;
    }

    @Override
    public Cursor query(Uri uri, String[] strArr, String str, String[] strArr2, String str2) {
        String str3;
        String str4;
        String str5;
        SQLiteDatabase readableDB = getReadableDB();
        String queryParameter = uri.getQueryParameter(QUERY_PARAM_LIMIT);
        String queryParameter2 = uri.getQueryParameter(QUERY_PARAM_OFFSET);
        String queryParameter3 = uri.getQueryParameter(QUERY_PARAM_GROUP_BY);
        if (TextUtils.isEmpty(queryParameter)) {
            queryParameter = null;
        } else if (!TextUtils.isEmpty(queryParameter2)) {
            queryParameter = queryParameter2 + "," + queryParameter;
        }
        String str6 = queryParameter;
        switch (uriMatcher.match(uri)) {
        case 1:
            return readableDB.query(PATH_WORKOUT, strArr, str, strArr2, queryParameter3, null, str2, str6);
        case 2:
            String[] appendArgs = appendArgs(strArr2, uri.getLastPathSegment());
            if (TextUtils.isEmpty(str)) {
                str3 = "id=?";
            } else {
                str3 = Separators.LPAREN + str + ") AND id=?";
            }
            return readableDB.query(PATH_WORKOUT, strArr, str3, appendArgs, queryParameter3, null, str2);
        case 3:
            return readableDB.query(PATH_TRACK_POINT, strArr, str, strArr2, queryParameter3, null, str2, str6);
        case 4:
            String[] appendArgs2 = appendArgs(strArr2, uri.getLastPathSegment());
            if (TextUtils.isEmpty(str)) {
                str4 = "id=?";
            } else {
                str4 = Separators.LPAREN + str + ") AND id=?";
            }
            return readableDB.query(PATH_TRACK_POINT, strArr, str4, appendArgs2, queryParameter3, null, str2);
        case 5:
            return readableDB.query(PATH_PACE_POINT, strArr, str, strArr2, queryParameter3, null, str2, str6);
        case 6:
            String[] appendArgs3 = appendArgs(strArr2, uri.getLastPathSegment());
            if (TextUtils.isEmpty(str)) {
                str5 = "id=?";
            } else {
                str5 = Separators.LPAREN + str + ") AND id=?";
            }
            return readableDB.query(PATH_PACE_POINT, strArr, str5, appendArgs3, queryParameter3, null, str2);
        default:
            throw new IllegalArgumentException("Unknown URI " + uri + " for query");
        }
    }

    @Override
    public int update(Uri uri, ContentValues contentValues, String str, String[] strArr) {
        int i;
        ContentResolver contentResolver;
        SQLiteDatabase writableDB = getWritableDB();
        String str2 = "id=?";
        switch (uriMatcher.match(uri)) {
        case 1:
            writableDB.update(PATH_WORKOUT, contentValues, str, strArr);
            i = 0;
            break;
        case 2:
            String[] appendArgs = appendArgs(strArr, uri.getLastPathSegment());
            if (!TextUtils.isEmpty(str)) {
                str2 = Separators.LPAREN + str + ") AND id=?";
            }
            i = writableDB.update(PATH_WORKOUT, contentValues, str2, appendArgs);
            break;
        case 3:
            i = writableDB.update(PATH_TRACK_POINT, contentValues, str, strArr);
            break;
        case 4:
            String[] appendArgs2 = appendArgs(strArr, uri.getLastPathSegment());
            if (!TextUtils.isEmpty(str)) {
                str2 = Separators.LPAREN + str + ") AND id=?";
            }
            i = writableDB.update(PATH_TRACK_POINT, contentValues, str2, appendArgs2);
            break;
        case 5:
            i = writableDB.update(PATH_PACE_POINT, contentValues, str, strArr);
            break;
        case 6:
            String[] appendArgs3 = appendArgs(strArr, uri.getLastPathSegment());
            if (!TextUtils.isEmpty(str)) {
                str2 = Separators.LPAREN + str + ") AND id=?";
            }
            i = writableDB.update(PATH_PACE_POINT, contentValues, str2, appendArgs3);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri + " for update");
        }
        if (i > 0 && (contentResolver = this.contentResolver) != null) {
            contentResolver.notifyChange(uri, null);
        }
        return i;
    }

    @Override
    public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> arrayList)
            throws OperationApplicationException {
        ContentProviderResult[] contentProviderResultArr;

        SQLiteDatabase writableDB = getWritableDB();
        writableDB.beginTransaction();
        try {
            contentProviderResultArr = super.applyBatch(arrayList);
            writableDB.setTransactionSuccessful();
        } finally {

            writableDB.endTransaction();

        }
        return contentProviderResultArr;
    }

    private SQLiteDatabase getWritableDB() {
        return LitePal.getDatabase();
    }

    private SQLiteDatabase getReadableDB() {
        return LitePal.getDatabase();
    }
}
